package com.qulai.amazon_us.mapper.basic;

import com.qulai.amazon_us.domain.basic.Image;
import com.qulai.amazon_us.domain.basic.dto.ImageDto;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 商品图片Mapper接口
 *
 * @author hunan
 * @date 2025-01-08
 */
public interface ImageMapper {
    /**
     * 查询商品图片
     *
     * @param id 商品图片主键
     * @return 商品图片
     */
    public Image selectImageById(Long id);


    @Select("select * from basic_image where basic_id = #{basicId}")
    public Image selectImageByBasicId(@Param("basicId") Long basicId);

    /**
     * 查询商品图片列表
     *
     * @param image 商品图片
     * @return 商品图片集合
     */
    public List<Image> selectImageList(ImageDto image);

    /**
     * 新增商品图片
     *
     * @param image 商品图片
     * @return 结果
     */
    public int insertImage(Image image);

    /**
     * 修改商品图片
     *
     * @param image 商品图片
     * @return 结果
     */
    public int updateImage(Image image);

    /**
     * 删除商品图片
     *
     * @param id 商品图片主键
     * @return 结果
     */
    public int deleteImageById(Long id);

    /**
     * 批量删除商品图片
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteImageByIds(Long[] ids);


    /**
     * 批量设置状态为0
     *
     * @param idsStr
     * @return
     */
    @Update("update basic_image set status = #{status},update_by = #{optName} , update_time = NOW() where id in (${idsStr})")
    public int batchUpdateStatus(@Param("idsStr") String idsStr, @Param("status") Integer status, @Param("optName") String optName);


    @Select("select basic_id from basic_image where id in (${ids})")
    public List<Long> selectBasicIdListByIds(@Param("ids") String ids);

    @Select("update basic_image set status = #{status},update_time = NOW()   where ${whereSql}")
    public void updateOfflineProduct(@Param("whereSql") String whereSql, @Param("status") Integer status);

    /**
     * 批量更新商品图片
     *
     * @param images 商品图片列表
     * @return 结果
     */
    @Update("<script>" +
            "UPDATE basic_image " +
            "SET " +
            "published = CASE " +
            "<foreach collection='images' item='image' separator=' '>" +
            "    WHEN basic_id = #{image.basicId} THEN #{image.published} " +
            "</foreach>" +
            "END, " +
            "update_time = NOW() " +
            "WHERE basic_id IN " +
            "<foreach collection='images' item='image' open='(' separator=',' close=')'>" +
            "    #{image.basicId} " +
            "</foreach>" +
            "</script>")
    int batchUpdatePublished(@Param("images") List<Image> images);

    Image getImageBySku(String sku);

}
